package com.bjpowernode.javase.collection.review;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/*
hashMap：
    初始化容量16，默认加载因子0.75，,扩容后的容量是原容量的二倍，
 */
public class HashMapTest {
    public static void main(String[] args) {
        Map<Integer,String> map = new HashMap<>();
        map.put(1,"lisi");
        map.put(0,"zhangsan");
        map.put(2,"wangwu");
        map.put(2,"shi,isi");//key重复value被覆盖
        System.out.println(map.size());

        //取：通过key取value
        System.out.println(map.get(2));

        //遍历map集合很重要
        //通过keySet()遍历
        Set<Integer> set = map.keySet();
        Iterator<Integer> it = set.iterator();
        while(it.hasNext()){
            Integer key = it.next();
            String value = map.get(key);
            System.out.println("key=" + key + " value=" + value);
        }

        //通过entrySet(),将map集合转换为set集合。set集合中每个数据是一个节点。，
        Set<Map.Entry<Integer,String>> set1 = map.entrySet();
        for(Map.Entry<Integer,String> mp : set1){
            System.out.println("key=" + mp.getKey() + " value=" + mp.getValue());
        }

        Iterator<Map.Entry<Integer,String>> it2 = set1.iterator();
        while(it2.hasNext()){
            Map.Entry<Integer,String> temp = it2.next();
            System.out.println("key==" + temp.getKey() + " value==" + temp.getValue());
        }


    }
}
